import { TDocumentDefinitions } from "pdfmake/interfaces";

import { pdfConfig, chargesDocConfig, constantData } from './PDF.config';

type ConfigType = {
    [key: string]: any
}

class PDFInsuranceInstituationalClaim {
    getPDFDefinition(claim: ConfigType) {
        let newClaim: ConfigType = {
            ...constantData,
            ...claim
        }
        let dbDocDefinitionTemp: any[] = [];
        let chargesDbDocDefinitionTemp: any[] = [];
        newClaim.charges.forEach((item: ConfigType, chargeIndex: number) => {
            Object.keys(item).forEach(key => {
                chargesDbDocDefinitionTemp.push({
                    text: item[key],
                    noWrap: true,
                    style: 'style',
                    absolutePosition: {
                        x: (chargesDocConfig[key]),
                        y:  (928 + chargeIndex * 51) * 0.250
                    }
                })
            })
        })
        const docDefinitionTemp = Object.keys(pdfConfig).map(key => { 
            if(!!pdfConfig[key][3]) {
                dbDocDefinitionTemp.push({
                    text: newClaim[key],
                    noWrap: true,
                    style: 'style',
                    absolutePosition: {
                        x: pdfConfig[key][2],
                        y: pdfConfig[key][3],
                    }
                })
            }
            return {
                text: newClaim[key],
                noWrap: true,
                style: 'style',
                absolutePosition: {
                    x: pdfConfig[key][0],
                    y: pdfConfig[key][1],
                }
            }
         })
        let docDefinition: TDocumentDefinitions = {
            content: [
                {
                    image: './ub04-1.jpg',
                    width: 595,
                    height: 842,
                    absolutePosition: { x: 0, y: 0 },
                },
                ...docDefinitionTemp,
                ...dbDocDefinitionTemp,
                ...chargesDbDocDefinitionTemp
            ],
            styles: {
                style: {
                  fontSize: 8,
                }
            }
        };
        return docDefinition;
    }
}

export default PDFInsuranceInstituationalClaim;
